﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevelopTool.Bll.LocalDbBlls;
using DevelopTool.Common;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.LuckDrawModels;
using DevelopTool.Model.NoteModels;
using LiteDB;
using Sunny.UI;

namespace DevelopTool.Bll.NoteBlls
{
    /// <summary>
    /// 笔记操作
    /// </summary>
    public class NoteBll : BaseBll
    {
        public NoteBll()
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Note>();
                col.EnsureIndex(x => x.NodeName);
                col.EnsureIndex(x => x.NodePid);
                col.EnsureIndex(x => x.NodeType);
                col.EnsureIndex(x => x.CreateTime);
                col.EnsureIndex(x => x.Id);
            }
        }

        public List<Note> GetList(MenuNodeType nodeType = MenuNodeType.Unknown)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var models = db.GetCollection<Note>().FindAll();

                if(nodeType != MenuNodeType.Unknown)
                {
                    models = models.Where(it => it.NodeType == nodeType);
                }

                return models.OrderByDescending(it => it.CreateTime).ToList();
            }
        }


        /// <summary>
        /// 根据笔记内容关键字查询到笔记id集合
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<Note> GetListByText(string text)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                List<Note> noteList = new List<Note>();

                var noteCol = db.GetCollection<Note>();

                var noteIdList = noteCol.Find(it => !string.IsNullOrEmpty(it.Text) && it.Text.GZipDecompress().IndexOf(text,StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.Id).ToList();
                foreach(int noteId in noteIdList)
                {
                    noteList.Add(noteCol.FindOne(it => it.Id == noteId && it.NodeType == MenuNodeType.Node));//只查询节点，不要目录
                }

                return noteList;
            }
        }


        /// <summary>
        /// 根据笔记附件文件名查询到笔记id集合
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<Note> GetListByFileName(string fileName)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                List<Note> noteList = new List<Note>();

                var noteFileCol = db.GetCollection<NoteFile>();

                var noteCol = db.GetCollection<Note>();

                var noteIdList = noteFileCol.Find(it => !string.IsNullOrEmpty(it.Filename) && it.Filename.IndexOf(fileName,StringComparison.OrdinalIgnoreCase) > -1).Select(it => it.NoteId).ToList();

                foreach(int noteId in noteIdList)
                {
                    noteList.Add(noteCol.FindOne(it => it.Id == noteId && it.NodeType == MenuNodeType.Node));////只查询节点，不要目录
                }
                return noteList;
            }
        }


        /// <summary>
        /// 根据id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Note GetById(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Note>();

                var note = col.FindOne(it => it.Id == id);

                //解压缩内容
                if(!string.IsNullOrEmpty(note.Text))
                    note.Text = note.Text.GZipDecompress();

                return note;

            }
        }


        /// <summary>
        /// 保存笔记
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public Note Save(Note note)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Note>();

                //压缩内容
                if(!string.IsNullOrEmpty(note.Text))
                    note.Text = note.Text.GZipCompress();

                note.UpdateTime = DateTime.Now;
                col.Upsert(note);
                return col.FindById(note.Id);
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var noteCol = db.GetCollection<Note>();

                //查询此笔记是否有子节点，若有子节点，则不删除。
                if(noteCol.Exists(it => it.NodePid == id)) return false;

                //删除对应的笔记附件
                var noteFileCol = db.GetCollection<NoteFile>();
                noteFileCol.Delete(it => it.NoteId == id);

                //删除笔记
                var result = noteCol.Delete(id);

                return result;
            }
        }


        public void SetRemark(int id,string remark)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                var col = db.GetCollection<Note>();

                var model = col.FindOne(it => it.Id == id);

                model.Remark = remark;
                model.UpdateTime = DateTime.Now;

                col.Update(model);
            }
        }

        public ManageMenu Get(int id)
        {
            using(var db = new LiteDatabase(LiteDbContext.ConnectionString))
            {
                return db.GetCollection<Note>().FindOne(it => it.Id == id);
            }
        }
    }
}
